package com.hmkj.web.controller.my;

import com.hmkj.common.entity.ResultEntity;
import com.hmkj.common.page.BasePageParams;
import com.hmkj.common.page.Pages;
import com.hmkj.common.utils.StringUtils;
import com.hmkj.core.exception.BussinessException;
import com.hmkj.core.mapper.system.NoticeLogMapper;
import com.hmkj.core.po.system.NoticeLog;
import com.hmkj.core.po.user.User;
import com.hmkj.core.service.system.NoticeLogService;
import com.hmkj.web.controller.GenericController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * ......................我佛慈悲......................
 * ......................_oo0oo_.......................
 * .....................o8888888o......................
 * .....................88" . "88......................
 * .....................(| -_- |)......................
 * .....................0\  =  /0......................
 * ...................___/`---'\___....................
 * ..................' \\|     |// '...................
 * ................/ \\|||  :  |||// \.................
 * .............../ _||||| -卍-|||||- \................
 * ..............|   | \\\  -  /// |   |...............
 * ..............| \_|  ''\---/''  |_/ |...............
 * ..............\  .-\__  '-'  ___/-. /...............
 * ............___'. .'  /--.--\  `. .'___.............
 * .........."" '<  `.___\_<|>_/___.' >' ""............
 * ........| | :  `- \`.;`\ _ /`;.`/ - ` : | |.........
 * ........\  \ `_.   \_ __\ /__ _/   .-` /  /.........
 * ....=====`-.____`.___ \_____/___.-`___.-'=====......
 * ......................`=---='.......................
 * ....................................................
 * .................佛祖开光 ,永无BUG...................
 */
@Api(description = "消息管理相关")
@Slf4j
@RestController
@RequestMapping(GenericController.MEMBER_URL + "/message")
public class MyMessageController extends GenericController<Integer, User> {
    @Resource
    private NoticeLogService noticeLogService;

    @ApiOperation(value = "个人中心-消息管理-消息列表", notes = "消息列表")
    @ResponseBody
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public ResultEntity<Pages<NoticeLog>> messageList(@ModelAttribute BasePageParams basePageParams) {
        User user = getLoginUser();
        Map<String, Object> param = new HashMap<>();
        param.put("userId", user.getId());
        return ok(noticeLogService.selectByPage(basePageParams,
                q -> ((NoticeLogMapper) q).findCountForNoticeLogList(param)
                , q -> ((NoticeLogMapper) q).findListForNoticeLogList(param)));
    }

    @ApiOperation(value = "个人中心-消息管理-批量删除消息", notes = "批量删除消息")
    @ResponseBody
    @RequestMapping(value = "del", method = RequestMethod.POST)
    public ResultEntity deleteMessage(@ApiParam("要删除的站内信的id，多个id以','隔开") @RequestParam(required = true) String ids) {
        if (!StringUtils.isNotBlank(ids)) {
            throw new BussinessException("请选择要删除的消息");
        }
        Pattern pattern = Pattern.compile("[0-9]{1,}");
        Matcher matcher = pattern.matcher(ids.replace(",", ""));
        if (!matcher.matches()) {
            throw new BussinessException("参数有误，请重新选择");
        }
        User user = getLoginUser();
        String delIds = "(" + ids + ")";
        try {
            noticeLogService.deleteNoticeLogs(user.getId(), delIds);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BussinessException("操作有误请重新选择");
        }
        return ok();
    }

    @ApiOperation(value = "个人中心-消息管理-批量标记消息", notes = "批量标记消息")
    @ResponseBody
    @RequestMapping(value = "edit", method = RequestMethod.POST)
    public ResultEntity doReadMessage(@ApiParam("要操作的站内信的id，多个id以','隔开") @RequestParam String ids,
                                      @ApiParam("操作类型： 1 标记已读， 2 标记未读") @RequestParam Integer type) {
        if (!StringUtils.isNotBlank(ids)) {
            throw new BussinessException("请选择要操作的消息");
        }
        Pattern pattern = Pattern.compile("[0-9]{1,}");
        Matcher matcher = pattern.matcher(ids.replace(",", ""));
        if (!matcher.matches()) {
            throw new BussinessException("参数有误，请重新选择");
        }
        User user = getLoginUser();
        String delIds = "(" + ids + ")";
        Integer readFlag;
        if (type == 1) {//已读
            readFlag = 1;
        } else {//未读
            readFlag = 2;
        }
        try {
            noticeLogService.updateNoticeLogsToRead(user.getId(), delIds, readFlag);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BussinessException("操作有误请重新选择");
        }
        return ok();
    }
}